package com.wcgksoft.controller.plant;


import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.Plant.PlantType;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.plant.PlantTypeService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 植物类型
 *
 * @author Administrator
 */
@Controller
@RequestMapping("planttype")
public class PlantTypeController {

    @Autowired
    private PlantTypeService plantTypeService;


    /**
     * 列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(PlantType.class);
        criteria.add(Restrictions.eq("enterpriseid", sysUser.getEnterprise().getId()));
        List<PlantType> list = plantTypeService.findByConditions(criteria, page, limit);
        return Layui.data(0, "查询成功", plantTypeService.getTotalCount(), list);
    }

    /*
     * 修改
     *
     * */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(HttpServletRequest request, @RequestParam(value = "data", required = false) String data) throws Exception {
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            PlantType plantType = (PlantType) JSONObject.toBean(JSONObject.fromObject(data), PlantType.class);
            String ids = String.valueOf(param.get("id"));
            if (ids.equals("")) {
                plantType.setState(0);
            }

            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
            plantType.setEnterpriseid(sysUser.getEnterprise().getId());
            plantTypeService.saveOrUpdate(plantType);
            return Layui.data(0, "修改成功", 1, Layui.addList(plantType));
        }
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="stop")
    public Layui stop(@RequestParam(value = "id") Integer id){
        PlantType byId = plantTypeService.findById(id);
        byId.setState(1);
        plantTypeService.saveMerge(byId);
        return Layui.data(1,"停用",1,null);
    }

    /**
     *  启用
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="start")
    public Layui start(@RequestParam(value = "id") Integer id){
        PlantType byId = plantTypeService.findById(id);
        byId.setState(0);
        plantTypeService.saveMerge(byId);
        return Layui.data(1,"启用",1,null);
    }


}
